﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////


#include "ConstraintPanel.h"
#include "GnufToolbar.h"
#include "GnufBlock.h"
#include "GnufPushButton.h"
#include "GnufInfo.h"
#include "GnufQtToolbar.h"
#include "GnufAppearanceSetting.h"
#include "GnufArrangementArea.h"
#include "GnufPallet.h"
#include "GnufUtility.h"
#include "GnufArrangementAreaEvents.h"
#include "GnufPreferences.h"
#include "GnufPreferenceEntries.h"
#include "GnufButtonControlTracker.h"
#include "GnufDropDownBoard.h"
#include "GnufDynamicZoneButtonStylePreferenceEntry.h"
#include "GnufWorkBench.h"
#include "GnufQtDropDownBoard.h"
#include <QMouseEvent>

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gnuf;

namespace {
    static const std::wstring s_key(L"ConstraintPanel");
    static ConstraintPanel s_constraintPanel;
}

//_____________________________________________________________________________
// ConstraintPanel class implementation
//
ConstraintPanel::ConstraintPanel()
    : DynamicPanelBase(s_key, gcmp::UniqueString::Find("ConstraintPanel"), L"ConstraintPanel.xml", true)
{
    //m_buttonStyle = DynamicZoneButtonStyleMgr::readAssistantPanelButtonStyleEntry();
}

ConstraintPanel::~ConstraintPanel()
{}

QWidget *
ConstraintPanel::qtWidget() 
{
    if (nullptr != m_pallet && nullptr != m_pallet->qtWidget()) {
        return m_pallet->qtWidget();
    }
    return nullptr;
}

void ConstraintPanel::onPostOpacityChanged()
{
    if (qtWidget()) {
        if (arrangementArea())
        {
            qtWidget()->setWindowOpacity(arrangementArea()->opacity());
        }
    }
}

void ConstraintPanel::onPostConnectedWithArrangementArea()
{
    Component::onPostConnectedWithArrangementArea();

    m_pallet = NEW_AS_OWNER_PTR(Pallet, arrangementArea());
    m_pallet->setEventHander(this);
    m_pallet->rHeader().caption = GBMP_TR(L"约束");  // 助手对话框
    m_pallet->rHeader().wantBackground = true;
    m_pallet->rHeader().canDragAndMove = false;
    m_pallet->rHeader().addButton(NEW_AS_OWNER_PTR(gnuf::Info, L"更多", L"更多", L":/images/More/Horizontal"));
    m_pallet->setWantBorder(true);
    if (rawToolbar())
    {
        m_pallet->setQtContent(rawToolbar()->qtItem());
    }
    m_pallet->show();

    Utility::setQtWidgetParent(this);
}

void ConstraintPanel::onPreDisconnectedFromArrangementArea()
{
    if (nullptr != m_contextMenu.get())
    {
        m_contextMenu->setVisible(false);
    }
    m_contextMenu.reset(nullptr);
    Component::onPreDisconnectedFromArrangementArea();

    m_pallet->hide(true);
    m_pallet.reset(nullptr);
}

void ConstraintPanel::onPostAbilityChanged()
{
    if (gnuf::Toolbar * tb = rawToolbar())
        if (QWidget * qtWgt = tb->qtItem())
            qtWgt->setEnabled(isEnabled());
}

void ConstraintPanel::initialize(Toolbar * toolbar)
{
    if (!toolbar)
        return;

    gcmp::OwnerPtr< AppearanceSetting > setting = NEW_AS_OWNER_PTR(
        AppearanceSetting, AppearanceSetting::EStyle::eAssistantPanel);
    DynamicPanelBase::setButtonDisplayStyle(buttonDisplayStyle(), true);
    toolbar->setEventHandler(this);
    toolbar->setAppearanceSetting(TransferOwnership(setting));

    for (auto& one : toolbar->rawContents()) {
        if (Block* block = one->cast<Block>()) {
            block->setLayoutType(Block::ELayoutType::eGridLayoutByRow);
        }
    }
}

gcmp::OwnerPtr<QtToolbar> ConstraintPanel::createQtToolBar(Toolbar* toolbar)
{
    return NEW_AS_OWNER_PTR(QtToolbar, toolbar, Direction::Vertical);
}

bool ConstraintPanel::onPreClicked(Item * item)
{
    if (ButtonControlTracker * pTracker = ButtonControlTracker::get()) {
        pTracker->trackInScenario(item,
            true);  // Affect the current context, as the user expects it.
    }

    return false; // continue to route...
}

void gnuf::ConstraintPanel::onHeightChanged(Toolbar* toolbar, int height)
{
    if (nullptr != m_pallet)
    {
        int headerHeight = m_pallet->headerHeight();
        if (arrangementArea())
        {
            const ArrangementArea::Padding& padding = arrangementArea()->padding();
            arrangementArea()->setExpectedHeight(
                padding.top +       // top padding
                headerHeight +      // the height of the header
                height +            // the height of the internal content
                padding.bottom +    // bottom padding
                2);                 // tolerance
            arrangementArea()->submit();
        }
    }
}

void ConstraintPanel::onEvent(ArrangementAreaEvent* evt)
{
    switch (evt->type())
    {
    case ArrangementAreaEvent::EType::ePreferencesChanged:
        auto theEvt = static_cast<PreferencesChangedAreaEvent*>(evt);
        const Preferences* preferences = theEvt->preferences();
        DBG_WARN_AND_RETURN_VOID_UNLESS(preferences, L"preferences is nullptr", L"paramodel", L"2024-02-20");

        if (const auto* entry = preferences->changedEntryBy(PreferenceEntry::eTheme))
        {
            if (m_pallet)
            {
                m_pallet->refresh();
            }
        }
        break;
    }
    Component::onEvent(evt);
}
